package com.example.digitaldiary;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.app.AlertDialog;
import android.support.v4.content.CursorLoader;
import android.util.Log;
import android.view.Menu;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.support.v4.content.CursorLoader;
public class SelectImgActivity extends Activity {

	private Cursor cursor;
	private int columnIndex = -1;
	private Context con;
	
	static {
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_img);
		con = this;
		// Tao mang lay ve ID cac cot trong bang Thmbnails
		String[] projection = {MediaStore.Images.Thumbnails._ID};
		// Tao con tro tro toi SD card
		CursorLoader loader = new CursorLoader(this, MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
				projection, null, null, 
				MediaStore.Images.Thumbnails.IMAGE_ID);

		cursor = loader.loadInBackground();

		if(cursor == null)
		{
			Log.i("Error roi", "cursor null");
		}
		else
		{
			Log.i("Ko null", cursor.toString());
		}
		// Lay ve index cua anh

		columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
		if(columnIndex < 0 )
		{
			Log.i("Error nua", "ko co anh");
		}
		else
		{
			Log.i("Co anh", String.valueOf(columnIndex));
		}
		GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
		sdcardImages.setAdapter(new ImageAdapter(this));

		// Click vao anh de lay duong dan

		sdcardImages.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position, long id) {
				// Lay duong dan
				String[] projection = {MediaStore.Images.Media.DATA};
				CursorLoader itemLoader = new CursorLoader(con,  MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
				cursor = itemLoader.loadInBackground();
				columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
				cursor.moveToPosition(position);
				// duong dan
				String imagePath = cursor.getString(columnIndex);


				AlertDialog alertDialog = new AlertDialog.Builder(con).create();
				alertDialog.setTitle("Show");
				alertDialog.setMessage(imagePath);
				alertDialog.show();
			}
		});

	}

	/**
	 * Adapter.
	 */
	private class ImageAdapter extends BaseAdapter {

		private Context context;

		public ImageAdapter(Context localContext) {
			context = localContext;
		}

		@Override
		public int getCount() {
			return cursor.getCount();
		}
		@Override
		public Object getItem(int position) {
			return position;
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView picturesView;
			if (convertView == null) {
				picturesView = new ImageView(context);
				cursor.moveToPosition(position);
				// lay id cua anh
				int imageID = cursor.getInt(columnIndex);
				Log.i("cursor count",String.valueOf(getCount()));
				Log.i("ID anh", String.valueOf(imageID));
				
				// URI
				picturesView.setImageURI(Uri.withAppendedPath(
						MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
				picturesView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				picturesView.setPadding(8, 8, 8, 8);
				picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
			}
			else {
				picturesView = (ImageView)convertView;
			}
			return picturesView;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//		getMenuInflater().inflate(R.menu.activity_select_img, menu);
		return true;
	}

}
